// miniprogram/pages/mine/point/point.js
const app = getApp()
var common = require('../../../utils/common.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    pageNum: 1,
    pageSize: 10,
    hasNextPage: false,
    pointDetailList: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.startPullDownRefresh()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    var that = this
    if (!app.globalData.userInfo) {
      app.wxLogin()
    }
    //用户通过分享，直接进入
    app.userInfoReadyCallback = userInfo => {
      //用户信息保存到全局
      app.globalData.userInfo = userInfo
      that.getNewPointDetail()
      that.getTotalPoint()
    }
    that.getNewPointDetail()
    that.getTotalPoint()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var that = this
    if (that.data.hasNextPage) {
      that.getMorePointDetail()
    }
  },
  onPageScroll: function (e) {
    this.setData({
      scrollTop: e.scrollTop
    })
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  getTotalPoint: function () {
    var that = this
    wx.request({
      url: `${common.baseUrl()}/pointDetail/findTotalPoint`,
      method: "GET",
      header: {
        "Content-Type": "application/x-www-form-urlencoded",
        "Authorization": app.globalData.token
      },
      data: {
        userId: app.globalData.userInfo.id
      },
      success(res) {
        console.log(res)
        if (res.data.flag) {
          that.setData({
            totalPoint: res.data.data
          })
        }
      },
      fail(res) {
        console.log(res)
      }
    })
  },
  getNewPointDetail: function () {
    var that = this
    that.setData({
      pageNum: 1
    })
    wx.request({
      url: `${common.baseUrl()}/pointDetail/findUserPointDetail/${that.data.pageNum}/${that.data.pageSize}`,
      method: "GET",
      header: {
        "Content-Type": "application/x-www-form-urlencoded",
        "Authorization": app.globalData.token
      },
      data: {
        userId: app.globalData.userInfo.id
      },
      success(res) {
        console.log(res)
        if (res.data.flag) {
          that.setData({
            pointDetailList: res.data.data.list,
            hasNextPage: res.data.data.hasNextPage
          })
          if (res.data.data.hasNextPage) {
            that.setData({
              pageNum: res.data.data.nextPage
            })
          }
        }
        wx.stopPullDownRefresh()
      },
      fail(res) {
        console.log(res)
      }
    })
  },
  getMorePointDetail: function () {
    var that = this
    wx.showLoading({
      title: '正在加载'
    })
    wx.request({
      url: `${common.baseUrl()}/pointDetail/findUserPointDetail/${that.data.pageNum}/${that.data.pageSize}`,
      method: "GET",
      header: {
        "Content-Type": "application/x-www-form-urlencoded",
        "Authorization": app.globalData.token
      },
      data: {
        userId: app.globalData.userInfo.id
      },
      success(res) {
        wx.hideLoading()
        if (res.data.flag) {
          that.setData({
            pointDetailList: that.data.pointDetailList.concat(res.data.data.list),
            hasNextPage: res.data.data.hasNextPage
          })
          if (res.data.data.hasNextPage) {
            that.setData({
              pageNum: res.data.data.nextPage
            })
          }
        }
      },
      fail(res) {
        console.log(res)
      }
    })
  },
  addClick: function (e) {
    wx.navigateTo({
      url: 'createPoint/createPoint',
    })
  }
})